package util;

import app.MConstants;

import com.wolfram.jlink.KernelLink;
import com.wolfram.jlink.MathLinkException;
import com.wolfram.jlink.MathLinkFactory;

public class MathUtil {
	static KernelLink ml = null;
	public static KernelLink getKernelLink() {
		String[] myArgv = new String[4];
		myArgv[0] = "-linkmode";
		myArgv[1] = "launch";
		myArgv[2] = "-linkname";
		myArgv[3] = MConstants.MATH_KERNEL_PATH;
		if(ml == null) {
			try {
				ml = MathLinkFactory.createKernelLink(myArgv);
				ml.discardAnswer();
			} catch (MathLinkException e) {
				e.printStackTrace();
			}
		}
		return ml;
	}
	
	public static void killKernelLink() {
		if (ml != null) {
			// Because we used the front end, it is important
			// to call CloseFrontEnd[] before closing the link.
			// Counterintuitively, this is not because we want
			// to force the front end to quit, but because we
			// _don't_ want to do this if the user has begun
			// working in the front end session we started.
			// CloseFrontEnd knows how to politely disengage
			// from the front end if necessary. The need for
			// this will go away in future releases of
			// Mathematica.
			ml.evaluateToInputForm("CloseFrontEnd[]", 0);
			ml.close();
			ml=null;
		}
	}

	public static int getColNum(int size) {
		// TODO Auto-generated method stub
		return (int) Math.ceil(Math.sqrt(size));
	}
}
